package com.newbsd.web.maventest.controller;


import java.awt.print.Pageable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newbsd.web.maventest.common.pojo.EasyUIDataGridJSON;
import com.newbsd.web.maventest.mvc.InnerDispatcherController;
import com.newbsd.web.maventest.pojo.Ticket;
import com.newbsd.web.maventest.service.TicketServicehx;
import com.newbsd.web.maventest.service.impl.TicketServiceImplhx;


/**
 * Servlet implementation class TicketController
 */
/**
 * 
 * @author 韩晓
 *
 */

public class TicketControllerhx extends InnerDispatcherController {
private static final long serialVersionUID = -8128936181765400012L;
   

    TicketServicehx  ticketService = new TicketServiceImplhx();
    EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
   

	public void index(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher("");
		dispatcher.forward(request, response);
	}

	
	//查询所有的页面
	public void lists(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		
		List<Ticket> tickets=ticketService.allTicket();
		
		response.setContentType("application/json");
		response.getWriter().append(JSON.toJSONString(tickets));
		System.out.println("lists  success"+JSON.toJSONString(tickets));
           list(request, response);
			}
	
	//分页查询	
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("111list");
		Integer	page;
		Integer rows;
		if (null ==request.getParameter("page") && null ==request.getParameter("rows") ) {
				page = 1;
				rows = 10;
		} 
		else {
			 page = Integer.valueOf(request.getParameter("page"));
			 rows = Integer.valueOf(request.getParameter("rows"));
		}
		
		
		//System.out.println("page:" + page + ";rows:" + rows);
		
		Integer total = ticketService.getTotals();
		
		
		List<Ticket> tickets = ticketService.allTicket(page, rows);
		
		responseObject.setTotal(total);
		responseObject.setRows(tickets);
		
		response.setContentType("application/json");
		response.getWriter().append(JSON.toJSONString(responseObject));
		
		System.out.println("分页成功");
		
	//	System.out.println("list"+JSON.toJSONString(responseObject));
	//	System.out.println("UserController list called.");
	}
   //增加车票信息
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 Ticket  ticket = new Ticket();
	     Integer  tickedid = Integer.parseInt(request.getParameter("tickedid"));
	     Integer  ticketnum = Integer.parseInt(request.getParameter("ticketnum"));
	     Double   price = Double.parseDouble(request.getParameter("price"));
		 String    origin = request.getParameter("origin");
		 String   destination =request.getParameter("destination");
		 String   trainno =request.getParameter("trainno");
		
		 ticket.setDestination(destination);
		 ticket.setOrigin(origin);
		 ticket.setPrice(price);
		 ticket.setTickedid(tickedid);
		 ticket.setTicketnum(ticketnum);
		 ticket.setTrainno(trainno);
		int i = ticketService.addTicket(ticket);
		 if (1==i) {
			 response.getWriter().append(JSON.toJSONString("success"));
				// lists(request, response);
				 System.out.println("add success "+ticket.toString()  );
		} else {
			response.getWriter().append(JSON.toJSONString("Error"));
			// lists(request, response);
			 System.out.println("add success "+ticket.toString()  );
		}
		 
		 
	}
	//更新车票信息
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	   
		Integer  tickedid = Integer.parseInt(request.getParameter("id"));
	     Integer  ticketnum = Integer.parseInt(request.getParameter("ticketnum"));
	     Double   price = Double.parseDouble(request.getParameter("price"));
		 String    origin = request.getParameter("origin");
		 String   destination =request.getParameter("destination");
		 String   trainno =request.getParameter("trainno");
		 Ticket  ticket = new Ticket();
		// System.out.println("更新ticket");
		 ticket.setDestination(destination);
		 ticket.setOrigin(origin);
		 ticket.setPrice(price);
		 ticket.setTickedid(tickedid);
		 ticket.setTicketnum(ticketnum);
		 ticket.setTrainno(trainno);
		 ticketService.updateTicket(ticket);
		 response.getWriter().append(JSON.toJSONString(ticket));
		 System.out.println( "updatesuccess");
		 
	}
	//删除车票信息
    public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Integer  tickedid = Integer.parseInt(request.getParameter("id"));
		 ticketService.delTicket(tickedid);
		 System.out.println("deletesuccess");
	}
	
}